//
//  ViewController.m
//  QQMusic_MV
//
//  Created by dllo on 16/8/15.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kTableViewReuse @"reuse"
#import "AFNetworking.h"
#import "FirstModel.h"
#import "DataModel.h"
#import "ContentsModel.h"
#import "ListModel.h"
#import "MVTableViewCell.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *qqMVTableView;
@property (nonatomic, strong) FirstModel *first;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createData];
}

- (void)createData {
    // 默认解析方式为json
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    [session GET:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *data = responseObject;
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        str = [str substringFromIndex:7];
        str = [str substringToIndex:str.length - 1];
//        str = [str stringByReplacingOccurrencesOfString:@"jsonp1(" withString:@""]; // 替换
        NSData *finalData = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:finalData options:0 error:nil];
        self.first = [FirstModel modelWithDic:dic];
        NSLog(@"%@", self.first.data.contents.list);
        dispatch_async(dispatch_get_main_queue(), ^{
        [self.qqMVTableView reloadData];
    });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];


}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"最新";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.first.data.contents.list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    cell.mvListArray = self.first.data.contents.list[indexPath.section].mvlist;
    
    return cell;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
